package top.chmin.project.cloud.sys.dict.data;

import java.util.List;
import java.util.ArrayList;
import java.time.LocalDateTime;

import top.chmin.project.cloud.tool.LocalTool;
import top.chmin.project.cloud.common.*;
import lombok.experimental.Accessors;
import lombok.Setter;
import lombok.Getter;



/**
 * chmin
 * 2024-01-26 14:02:41
 * cloud-code
 */
@Getter
@Setter
@Accessors(chain = true)
public class DictData extends Model {
    /*###########auto#start###########*/
    // 字典类型
    private String typeCode;
    // 字典键
    private String typeLabel;
    // 字典值
    private String typeValue;
    /*###########auto##end############*/
    /* append $modelFieldAppender$ */

    public DictDataModelCondition forInsert() {
        return new DictDataModelCondition().forInsert();
    }

    public DictDataModelCondition forUpdate() {
        return new DictDataModelCondition().forUpdate();
    }

    public DictDataModelCondition forDelete() {
        return new DictDataModelCondition().forDelete();
    }

    public DictDataModelCondition forSelect() {
        return new DictDataModelCondition().forSelect();
    }

    public class DictDataModelCondition extends Condition {
        DictDataModelCondition parent = null;

        private DictDataModelCondition() {
        }

        private DictDataModelCondition(DictDataModelCondition condition) {
            this.parent = condition;
        }

        private DictDataModelCondition forInsert() {
            DictDataModelCondition condition = new DictDataModelCondition();
            List<String> columnNameList = new ArrayList<>();
            List<String> valueList = new ArrayList<>();
            /*###########auto#start###########*/
            columnNameList.add("type_code");
            valueList.add("?");
            condition.getValueList().add(getTypeCode());

            columnNameList.add("type_label");
            valueList.add("?");
            condition.getValueList().add(getTypeLabel());

            columnNameList.add("type_value");
            valueList.add("?");
            condition.getValueList().add(getTypeValue());

            /*###########auto##end############*/
            /* append $modelForInsertFieldAppender$ */
            condition.getValueList().add(LocalTool.getUserId());
            condition.getValueList().add(LocalTool.getUserId());
            condition.getValueList().add(LocalDateTime.now());
            condition.getValueList().add(LocalDateTime.now());
            condition.setSql("INSERT INTO sys_dict_data(%s, create_by, update_by, create_time, update_time) VALUES (%s, ?, ?, ?, ?)".formatted(
                    String.join(", ", columnNameList),
                    String.join(", ", valueList)
            ));
            return condition;
        }

        private DictDataModelCondition forUpdate() {
            DictDataModelCondition condition = new DictDataModelCondition();
            /*###########auto#start###########*/
            if (typeCode != null) {
                condition.getUpdateList().add("type_code = ?");
                condition.getUpdateValueList().add(typeCode);
            }

            if (typeLabel != null) {
                condition.getUpdateList().add("type_label = ?");
                condition.getUpdateValueList().add(typeLabel);
            }

            if (typeValue != null) {
                condition.getUpdateList().add("type_value = ?");
                condition.getUpdateValueList().add(typeValue);
            }

            /*###########auto##end############*/
            /* append $modelForUpdateFieldAppender$ */
            if (getUpdateBy() != null) {
                condition.getUpdateList().add("update_by = ?");
                condition.getValueList().add(LocalTool.getUserId());
            }
            if (getUpdateTime() != null) {
                condition.getUpdateList().add("update_time = ?");
                condition.getValueList().add(LocalDateTime.now());
            }
            condition.setSql("UPDATE sys_dict_data#update##where##order##limit#");
            return condition;
        }

        private DictDataModelCondition forDelete() {
            DictDataModelCondition condition = new DictDataModelCondition();
            condition.setSql("DELETE FROM sys_dict_data#where##group##order##limit#");
            return condition;
        }

        private DictDataModelCondition forSelect() {
            DictDataModelCondition condition = new DictDataModelCondition();
            condition.modelClass = DictData.class;
            condition.setSql("SELECT #select# FROM sys_dict_data#where##group##order##limit#");
            return condition;
        }

        public DictDataModelCondition distinct() {
            this.distinct = true;
            return this;
        }

        public DictDataModelCondition limit(int limit) {
            this.limit = " LIMIT " + limit;
            return this;
        }

        public DictDataModelCondition sub() {
            return new DictDataModelCondition(this);
        }

        public DictDataModelCondition finish() {
            if (!whereList.isEmpty()) {
                parent.whereList.add("(" + String.join(" OR ", whereList) + ")");
            }
            parent.whereValueList.addAll(whereValueList);
            return parent;
        }

        public DictDataCondition toFlex() {
            DictDataCondition flex = new DictDataCondition();
            flex.setSql(sql);
            flex.setDistinct(distinct);
            flex.setModelClass(modelClass);
            flex.setSelectList(selectList);
            flex.setUpdateList(updateList);
            flex.setUpdateValueList(updateValueList);
            flex.setWhereList(whereList);
            flex.setWhereValueList(whereValueList);
            flex.setGroupList(groupList);
            flex.setOrderList(orderList);
            flex.setLimit(limit);
            return flex;
        }

        /*###########auto#start###########*/
        public CreateBy createBy() {
            return new CreateBy();
        }

        public class CreateBy extends StringModelColumn<DictDataModelCondition> {
            private CreateBy() {
                super(DictDataModelCondition.this, "create_by", DictData.this.getCreateBy());
            }
        }

        public CreateTime createTime() {
            return new CreateTime();
        }

        public class CreateTime extends LocalDateTimeModelColumn<DictDataModelCondition> {
            private CreateTime() {
                super(DictDataModelCondition.this, "create_time", DictData.this.getCreateTime());
            }
        }

        public Id id() {
            return new Id();
        }

        public class Id extends IntegerModelColumn<DictDataModelCondition> {
            private Id() {
                super(DictDataModelCondition.this, "id", DictData.this.getId());
            }
        }

        public TypeCode typeCode() {
            return new TypeCode();
        }

        public class TypeCode extends StringModelColumn<DictDataModelCondition> {
            private TypeCode() {
                super(DictDataModelCondition.this, "type_code", DictData.this.getTypeCode());
            }
        }

        public TypeLabel typeLabel() {
            return new TypeLabel();
        }

        public class TypeLabel extends StringModelColumn<DictDataModelCondition> {
            private TypeLabel() {
                super(DictDataModelCondition.this, "type_label", DictData.this.getTypeLabel());
            }
        }

        public TypeValue typeValue() {
            return new TypeValue();
        }

        public class TypeValue extends StringModelColumn<DictDataModelCondition> {
            private TypeValue() {
                super(DictDataModelCondition.this, "type_value", DictData.this.getTypeValue());
            }
        }

        public UpdateBy updateBy() {
            return new UpdateBy();
        }

        public class UpdateBy extends StringModelColumn<DictDataModelCondition> {
            private UpdateBy() {
                super(DictDataModelCondition.this, "update_by", DictData.this.getUpdateBy());
            }
        }

        public UpdateTime updateTime() {
            return new UpdateTime();
        }

        public class UpdateTime extends LocalDateTimeModelColumn<DictDataModelCondition> {
            private UpdateTime() {
                super(DictDataModelCondition.this, "update_time", DictData.this.getUpdateTime());
            }
        }

        /*###########auto##end############*/
        /* append $modelConditionFieldAppender$ */
    }
}
